package com.hbyj.struts.action.authority;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.record.formula.functions.Request;
import org.apache.struts2.ServletActionContext;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Departments;
import com.hbyj.hbm.vo.MemberRole;
import com.hbyj.hbm.vo.Members;
import com.hbyj.hbm.vo.Roles;


import com.hbyj.object.DepartmentForFlexGrid;
import com.hbyj.object.FlexGridData;
import com.hbyj.object.FlexiGridData;
import com.hbyj.object.IFlexiGridable;
import com.hbyj.object.MemberForFlexiGrid;
import com.hbyj.object.MemberRoleForFlexiGrid;
import com.hbyj.object.MemberUpdateObject;
import com.hbyj.object.RoleAddData;
import com.hbyj.object.RoleDepartmetnList;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AuthorityMemberAction extends ActionSupport {
	private String jsonResult;
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	
	public void bindData_query() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String regionCode = request.getParameter("code");
		
		List listRoles = DbTools.findByHql("from Roles");
		List listDepartments = DbTools.findByHql("from Departments departments where departments.regionCode='"
				+regionCode+"'");
		
		RoleDepartmetnList list = new RoleDepartmetnList(listRoles, listDepartments);

		String  jsonText= JSONObject.fromObject(list).toString();

		 ServletActionContext.getResponse().getOutputStream().write(jsonText.getBytes("utf-8"));
	}
	
	public void member_add() {
		try {
			    
			HttpServletRequest request = ServletActionContext.getRequest();
			String memberName =request.getParameter("member_name");
			String memberDepartmentIdString=request.getParameter("member_department_id");
			String memberRoleIdString=request.getParameter("member_role_id");
			String memberPsw=request.getParameter("member_psw");
			String memberNickName=request.getParameter("member_nickname");
			String memberRegionCodeString = request.getParameter("code");
			if(memberName=="" || memberDepartmentIdString=="" ||memberRoleIdString==""){
				ServletActionContext.getResponse().getOutputStream().write("缺少参数请检查".getBytes("utf-8"));
				return;
			}
		    int memberDepartmentId =Integer.parseInt(memberDepartmentIdString);
			
		    int memberRoleId = Integer.parseInt(memberRoleIdString);
		    
		    List listDepartmentsHiberList = DbTools.findByHql("from Departments departments where departments.id= "+memberDepartmentId);
		    List listRolesHiberList = DbTools.findByHql("from Roles roles where roles.id= "+memberRoleId);
		    Members member = new Members(null,(Departments)listDepartmentsHiberList.get(0),memberNickName,memberName,memberPsw);
		    member.setLyCode(memberRegionCodeString);
		    DbTools.saveObj(member);
		    
		    
		    MemberRole memberRole = new MemberRole(member.getId(),(Roles)listRolesHiberList.get(0),member);
		    DbTools.saveObj(memberRole);
		    ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
			//ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
		}
	}

	public void member_query(){
		try {
			
			HttpServletRequest request = ServletActionContext.getRequest();
			String flag = request.getParameter("flag");
			 Integer page =Integer.parseInt(request.getParameter("page"));
			 Integer rp = Integer.parseInt(request.getParameter("rp"));
			 
			 Integer start =(page-1)*rp;
			 Integer end =page * rp;
			  

			if(flag.equalsIgnoreCase("byName")){
				String memberName =request.getParameter("member_name");
				String hqlString="from Members ";
				if(!memberName.isEmpty()){
					hqlString+="members where members.empName='"+memberName+"'";
				}
				List listMemberTotal = DbTools.findByHql(hqlString);
				List listMember = DbTools.findPageHQL(start, end, hqlString);
				int membersTotalCount = listMemberTotal.size();
				int membersCount = listMember.size();
				if(membersTotalCount==0){
					return;
				}
				List<IFlexiGridable> flexiGridables = new ArrayList<IFlexiGridable>();
				for (int i = 0; i < membersCount; i++) {
					flexiGridables.add(new MemberForFlexiGrid());
				}
				FlexiGridData<MemberForFlexiGrid> datas = new FlexiGridData<MemberForFlexiGrid>(membersTotalCount,page, listMember, flexiGridables);
				String  rltString= JSONObject.fromObject(datas).toString();
				ServletActionContext.getResponse().getOutputStream().write(rltString.getBytes("utf-8"));
			}
			else{
				String department_name =request.getParameter("department_name");
				String role_name = request.getParameter("role_name");
				String hqlString="from Members";
				if(department_name.isEmpty()&&role_name.isEmpty()){
					
					List listMemberTotal = DbTools.findByHql(hqlString);
					List listMember = DbTools.findPageHQL(start, end, hqlString);
					int membersTotalCount = listMemberTotal.size();
					int membersCount = listMember.size();
					if(membersTotalCount==0){
						return;
					}
					List<IFlexiGridable> flexiGridables = new ArrayList<IFlexiGridable>();
					for (int i = 0; i < membersCount; i++) {
						flexiGridables.add(new MemberForFlexiGrid());
					}
					FlexiGridData<MemberForFlexiGrid> datas = new FlexiGridData<MemberForFlexiGrid>(membersTotalCount,page, listMember, flexiGridables);
					String  rltString= JSONObject.fromObject(datas).toString();
					ServletActionContext.getResponse().getOutputStream().write(rltString.getBytes("utf-8"));
				}
				if(!department_name.isEmpty()&&role_name.isEmpty()){
					hqlString="from Members";
					hqlString+=" members where members.departments.name='"+department_name+"'";
					List listMemberTotal = DbTools.findByHql(hqlString);
					List listMember = DbTools.findPageHQL(start, end, hqlString);
					int membersTotalCount = listMemberTotal.size();
					int membersCount = listMember.size();
					if(membersTotalCount==0){
						return;
					}
					List<IFlexiGridable> flexiGridables = new ArrayList<IFlexiGridable>();
					for (int i = 0; i < membersCount; i++) {
						flexiGridables.add(new MemberForFlexiGrid());
					}
					FlexiGridData<MemberForFlexiGrid> datas = new FlexiGridData<MemberForFlexiGrid>(membersTotalCount,page, listMember, flexiGridables);
					String  rltString= JSONObject.fromObject(datas).toString();
					ServletActionContext.getResponse().getOutputStream().write(rltString.getBytes("utf-8"));
				}
				if(!role_name.isEmpty()){
					hqlString="from MemberRole ";
					hqlString+="memberrole where memberrole.roles.name='"+role_name+"'";
					if(!department_name.isEmpty()){
						hqlString+=" and memberrole.members.departments.name='"+department_name+"'";
					}
					List listMemberTotal = DbTools.findByHql(hqlString);
					List listMember = DbTools.findPageHQL(start, end, hqlString);
					int membersTotalCount = listMemberTotal.size();
					int membersCount = listMember.size();
					if(membersTotalCount==0){
						return;
					}
					List<IFlexiGridable> flexiGridables = new ArrayList<IFlexiGridable>();
					for (int i = 0; i < membersCount; i++) {
						flexiGridables.add(new MemberRoleForFlexiGrid());
					}
					FlexiGridData<MemberRoleForFlexiGrid> datas = new FlexiGridData<MemberRoleForFlexiGrid>(membersTotalCount,page, listMember, flexiGridables);
					String  rltString= JSONObject.fromObject(datas).toString();
					ServletActionContext.getResponse().getOutputStream().write(rltString.getBytes("utf-8"));
				}
			
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void member_update() throws UnsupportedEncodingException, IOException{
		  try {
			  HttpServletRequest request = ServletActionContext.getRequest();
			  String memberString = request.getParameter("data");
			   JSONObject jsonObj = JSONObject.fromObject(memberString);
			   MemberUpdateObject memberPost = (MemberUpdateObject)JSONObject.toBean(jsonObj,MemberUpdateObject.class);
			   Boolean rlt=DbTools.saveOrUpdateObj(memberPost.GetMembers());
			   ServletActionContext.getResponse().getOutputStream().write((rlt.toString()).getBytes("utf-8"));
		} catch (Exception e) {
			ServletActionContext.getResponse().getOutputStream().write(e.toString().getBytes("utf-8"));
			e.printStackTrace();
		}
	}

	public void member_delete() throws UnsupportedEncodingException, IOException{
		try{
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer member_id = Integer.parseInt(request.getParameter("member_id"));
		String hql = "delete from Members mem where mem.id="+member_id;
		Boolean rltBoolean=DbTools.delete(hql);
		ServletActionContext.getResponse().getOutputStream().write((rltBoolean.toString()).getBytes("utf-8"));
	} catch (Exception e) {
		e.printStackTrace();
		ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
	}
	}
}































